<template>
  <div class="A-reply">
    <div class="reply-header-name-replyName-del">
      <!-- 头像/名字/回复/删除 -->
      <div class="reply-header-name-replyName">
        <div class="reply-hader">
          <span>
            <router-link
              :to="{
                name: targetRoute,
                query: {
                  id: replyData.createUser,
                },
              }"
              target="_blank"
            >
              <img
                src="../../../assets/szptImages/images/builderHeader.png"
                alt=""
                v-if="!replyData.createUserHeadshotUrl ? true : false"
              />
              <img
                :src="replyData.createUserHeadshotUrl"
                alt=""
                v-if="replyData.createUserHeadshotUrl ? true : false"
              /> </router-link
          ></span>
        </div>
        <div class="reply-name">
          <span
            ><router-link
              :to="{
                name: targetRoute,
                query: {
                  id: replyData.createUser,
                },
              }"
              target="_blank"
            >
              {{ replyData.createUsername }}
            </router-link></span
          >
        </div>
        <div class="reply-replyname">
          <!-- 回复人的名字 -->
          <span>{{ getTargetUserMsg(replyData.targetUsername) }}</span>
        </div>
      </div>
      <div class="operation" v-show="opeateContion(replyData.createUser)">
        <div class="operate">...</div>
        <div class="oper-container">
          <span @click="CommentDel(replyData.id)">删除</span>
        </div>
      </div>
    </div>
    <div class="reply-container-box">
      <!-- 评论内容 -->
      <div class="post-msg">
        {{ replyData.content }}
      </div>
    </div>
    <div class="reply--time-appload-r">
      <!-- 时间/点赞/回复 -->
      <div class="reply-time">
        <span>{{ replyData.createTime }}</span>
      </div>
      <div class="reply-appload">
        <span @click="changeParse(replyData.id)">
          <i
            class="iconfont icon-dianzan applod"
            v-show="!likeFlag ? true : false"
          ></i>
          <i
            class="iconfont icon-dianzan applod"
            style="color: rgb(0, 195, 255)"
            v-show="likeFlag ? true : false"
          ></i>
          &emsp14;<span style="font-size: 16px">{{ likeCount }}</span></span
        >
        <span style="margin-left: 20px; font-size: 15px" @click="replyToUser()"
          >回复</span
        >
      </div>
    </div>
    <div class="reply-bulider-container" v-show="showFlag">
      <div class="post-comment-box">
        <div>
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="replyMsg"
            maxlength="500"
            show-word-limit
            class="comment-post-msg"
          >
          </el-input>
        </div>
        <div class="btn-box">
          <el-button
            type="primary"
            size="small"
            class="postMsg-btn"
            @click="replyBuilder(replyData.id)"
            >提交评论</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { componentForPost, delComment } from "@/api/szpt/commentForPost.js";
import { parse_collect_concern } from "@/api/szpt/parse";
export default {
  name: "Areply",
  props: {
    // 接受的从父组件传过来的回复的全部信息
    replyData: {
      require: true,
      default: Array,
    },
    initCommentList: {
      require: true,
    },
    // 回复的帖子id
    parentID: {
      require: true,
    },
    // 回复的用户名字ID
    builderUser: {
      require: true,
    },
    ComIndex: {
      require: true,
    },
  },
  data() {
    return {
      showFlag: false,
      replyMsg: "",
      targetRoute: "szptPersonalCenter",
      likeFlag: false,
      likeCount: 0,
    };
  },
  computed: {
    userMsg() {
      return this.$store.getters["userData/getUserDataMsg"];
    },
  },

  methods: {
    replyToUser() {
      this.showFlag = !this.showFlag;
    },
    // 改变点赞状态
    changeParse(id) {
      parse_collect_concern(id, "SZPT_SZCOMMENT_SYSUSER_LIKE")
        .then((resp) => {
          if (resp.code === 200) {
            if (this.likeFlag) {
              this.likeCount = this.likeCount - 1;
              this.$message.success("已取消点赞！");
            } else {
              this.likeCount = this.likeCount + 1;
              this.$message.success("点赞成功！");
            }
            this.likeFlag = !this.likeFlag;
          } else {
            this.$message.error("点赞状态更新失败！");
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    // 回复用户已有评论
    replyBuilder(ReplyToID, AcommentData) {
      if (this.replyMsg) {
        var commentData = {
          createUser: this.userMsg.data.userId,
          status: 0,
          parentId: this.parentID,
          content: this.replyMsg,
          targetId: ReplyToID,
          entityId: this.$route.query.id,
          entityType: "POST",
        };
        componentForPost(commentData)
          .then((resp) => {
            if (resp.code === 200 && resp.data) {
              this.$message.success("回复成功！");
              setTimeout(() => {
                this.replyMsg = "";
                this.$props.initCommentList("time");
              }, 800);
            }
          })
          .catch((err) => {
            console.log(err);
          });
        this.showFlag = !this.showFlag;
      } else {
        this.$modal.alertWarning("评论内容不能为空！");
      }
    },

    /**
     * @description 判断操作是否可以显示
     */
    opeateContion(createUser) {
      // console.log(this.userMsg);
      if (createUser) {
        if (this.userMsg) {
          // 判断用户是否登录，没有登录直接看不到
          //判断当前评论是否是用户评论的
          if (this.userMsg.data.userId === createUser) {
            return true;
          }
        } else {
          return false;
        }
      }
      return false;
    },
    // 获取回复用户的个人信息
    getTargetUserMsg(createUsername) {
      if (createUsername) {
        return "@" + createUsername;
      } else {
        return;
      }
    },
    /**
     * @description 删除用户评论
     */
    CommentDel(commentID) {
      if (commentID) {
        this.$confirm("此操作将永久删除该评论, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            delComment(commentID)
              .then((resp) => {
                if (resp.code === 200) {
                  this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                  setTimeout(() => {
                    this.$props.initCommentList("hot");
                  }, 800);
                } else {
                  this.$message({
                    type: "ereor",
                    message: "删除失败！!",
                  });
                }
              })
              .catch((err) => {
                this.$message({
                  type: "ereor",
                  message: "系统错误！!",
                });
              });
          })
          .catch((err) => {
            console.log(err);
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
  },
  mounted() {
    // console.log("二级评论", this.replyData);
    // console.log("二级评论",this.ComIndex);
    this.likeCount = parseInt(this.$props.replyData.parsecount);
    this.likeFlag = this.$props.replyData.islike;
  },
};
</script>

<style scoped lang="scss">
/**
   * 这里是一条回复的样式
   */
.A-reply {
  width: 100%;
  height: auto;
  background-color: #f7f8fc;
  margin-bottom: 20px;
  padding: 10px 10px;
  border-bottom: 1px solid #eceeec;
}
.reply-header-name-replyName-del {
  width: 100%;
  height: 30px;
  display: flex;
  justify-content: space-between;
}
.reply-container-box {
  width: 100%;
  height: auto;
  margin-top: 10px;
  color: #666;
  padding: 10px 10px 10px 40px;
}
.reply--time-appload-r {
  width: 100%;
  height: 30px;
  margin-top: 10px;
  font-size: 16px;
  padding-left: 45px;
  padding-right: 20px;
  line-height: 30px;
  display: flex;
  color: #666;
  justify-content: space-between;
}
.reply-header-name-replyName {
  height: 100%;
  width: auto;
  display: flex;
  line-height: 30px;
  color: #666;
  justify-content: flex-start;
}
.reply-hader {
  width: 30px;
  height: 30px;
  cursor: pointer;
  border-radius: 15px;
  overflow: hidden;
}
.reply-hader img {
  width: 100%;
  height: 100%;
}
.reply-name {
  cursor: pointer;
  font-weight: bold;
  margin-left: 10px;
}
.reply-replyname {
  margin-left: 20px;
  cursor: pointer;
  color: rgb(0, 207, 255);
}
.reply-appload span {
  cursor: pointer;
}
.reply-bulider-container {
  width: 100%;
  height: auto;
  padding-left: 20px;
  background-color: #f7f8fc;
  min-height: 100px;
}
.operate {
  cursor: pointer;
  width: 50px;
  text-align: center;
  position: relative;
  font-weight: bold;
  color: #666;
}
.operation:hover .oper-container {
  display: block;
}
.oper-container {
  width: 50px;
  height: auto;
  display: none;
  border-radius: 5px;
  overflow: hidden;
  color: #666;
  background-color: #fdfdfd;
}
.oper-container span {
  display: block;
  width: 100%;
  text-align: center;
  height: 30px;
  cursor: pointer;
  font-size: 16px;
  line-height: 30px;
}
.oper-container span:hover {
  color: red;
  background-color: #e8e6e6;
}
.btn-box {
  margin-top: 10px;
  display: flex;
  justify-content: flex-end;
}
.postMsg-btn {
  display: inline-block;
  position: relative;
  border-radius: 5px;
}
.applod,
.collected {
  font-size: 21px;
}
</style>
